package com.haierCamera.customapplication.ui.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklAdapterAlarmDeviceSelectBinding;
import com.haierCamera.customapplication.databinding.HzklAdapterIntradayMsgBinding;
import com.haierCamera.customapplication.ui.business.entity.AlarmMessageInfo;
import com.haierCamera.customapplication.ui.business.util.ImageHelper;
import com.haierCamera.customapplication.ui.custom.BaseListenerOnClik;
import com.haierCamera.customapplication.ui.custom.DataBoundViewHolder;
import com.haierCamera.customapplication.utils.DensityUtil;
import com.lechange.common.log.Logger;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

public class AlarmMessageList2Adapter extends RecyclerView.Adapter<DataBoundViewHolder<HzklAdapterIntradayMsgBinding>> {

    private List<AlarmMessageInfo> data;
    private Context context;
    private FragmentActivity activity;
    private BaseListenerOnClik click;
    private OnCheckSeleckListener onCheckSeleckListener;
    private boolean isEdit;

    public AlarmMessageList2Adapter(Context context) {
        this.context = context;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }

    public void upDate(List<AlarmMessageInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setOnItemOnClick(BaseListenerOnClik click) {
        this.click = click;
    }

    public void setOnCheckSeleckListener(OnCheckSeleckListener onCheckSeleckListener) {
        this.onCheckSeleckListener = onCheckSeleckListener;
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataSetChanged();
    }

    public List<AlarmMessageInfo> getData() {
        return data;
    }

    public void setData(List<AlarmMessageInfo> data) {
        this.data = data;
    }

    @Override
    public DataBoundViewHolder<HzklAdapterIntradayMsgBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//        Log.i("TAG","AlarmMessageList2Adapter_onCreateViewHolder");
        HzklAdapterIntradayMsgBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context)
                , R.layout.hzkl_adapter_intraday_msg, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<HzklAdapterIntradayMsgBinding> holder, int i) {
        Log.i("TAG","AlarmMessageList2Adapter-onBindViewHolder");
        holder.binding.tvName.setText(data.get(i).getName());
        holder.binding.tvContent.setText(data.get(i).getLocalDate().substring(10,data.get(i).getLocalDate().length()));
        Log.i("TAG","picUrl-" + data.get(i).getPicUrl());

        if(data.get(i).getPicUrl() != null && data.get(i).getPicUrl().length() > 0) {
            ImageHelper.loadCacheImage(data.get(i).getPicUrl(),data.get(i).getDeviceId()
                    ,data.get(i).getDeviceKey() != null ? data.get(i).getDeviceKey() : data.get(i).getDeviceId(), new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    try {
                        if(data.get(i).getPicUrl().hashCode() == msg.what && msg.obj != null){

                            BitmapDrawable bd = (BitmapDrawable) ((Drawable)msg.obj);
                            Bitmap image= bd.getBitmap();
                            //创建RoundedBitmapDrawable对象
                            RoundedBitmapDrawable roundImg = RoundedBitmapDrawableFactory.create(context.getResources(),image);
                            //抗锯齿
                            roundImg.setAntiAlias(true);
                            //设置圆角半径
                            roundImg.setCornerRadius(DensityUtil.dip2px(context,18));

//                                    holder.binding.ivScale.setImageDrawable((Drawable)msg.obj);
                            holder.binding.ivScale.setImageDrawable(roundImg);


                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        }
        if (isEdit) {
            holder.binding.roCb.setVisibility(View.VISIBLE);
        } else {
            holder.binding.roCb.setVisibility(View.GONE);
        }

        if (data.get(i).isSelect()){
            holder.binding.roCb.setChecked(true);
        } else {
            holder.binding.roCb.setChecked(false);
        }

        holder.binding.roCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Log.i("TAG","onCheckedChanged-" + i);
                if (b) {
                    data.get(i).setSelect(true);
                } else {
                    data.get(i).setSelect(false);
                }
                onCheckSeleckListener.onSelectChange(i,b);
            }
        });

        holder.itemView.setOnClickListener((v)->{
            click.onClick(i);
        });


    }

    private String getTypeStr(int t) {
        switch (t) {
            case 0:
                return "人体红外";

            case 1:
                return "动态检测";

            case 2:
                return "未知告警";

            case 3:
                return "低电压告警";

            case 4:
                return "配件人体红外检测";

            case 5:
                return "移动感应器发生移动事件";

            case 6:
                return "移动感应器长时间未发生移动事件";

            case 7:
                return "配件人体红外检测长久未报警事件";

            case 8:
                return "门磁报警事件";

            case 90:
                return "报警网关门磁探测报警";

            case 91:
                return "报警网关红外探测报警";

            case 92:
                return "报警网关低电量报警";

            case 93:
                return "报警网关防拆报警";

            default:
                return "";

        }
    }

    @Override
    public int getItemCount() {
        if (data!=null) {
            return data.size();
        } else {
            return 0;
        }
    }

    public interface OnCheckSeleckListener{
        void onSelectChange(int pos, boolean select);
    }
}
